package com.goods.controller.business;


import com.goods.common.model.business.Product;
import com.goods.common.model.business.Supplier;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.SupplierVO;
import com.goods.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/supplier")
@CrossOrigin
public class SupplierController {

    @Autowired
    SupplierService productService;


    //物资来源分页列表展示
    @GetMapping("/findSupplierList")
    public ResponseBean<Map<Object,Object>> findProductList(int pageSize, int pageNum,String name,String address,String contact){
        Map map = new HashMap();
        List<SupplierVO> list = productService.findProductList(name,address,contact);
        List<SupplierVO> productVOList = ListPageUtils.page(list,pageSize,pageNum);
        map.put("total",list.size());
        map.put("rows",productVOList);
        return ResponseBean.success(map);
    }

    //添加物资来源
    @PostMapping("/add")
    public ResponseBean add(@RequestBody SupplierVO SupplierVO){
        int i = productService.add(SupplierVO);
        if (i == 0){
            return ResponseBean.error("添加失败");
        }else {
            return ResponseBean.success();
        }
    }

    //编辑物资来源(数据回显)
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        Supplier Supplier = productService.edit(id);
        if (Supplier == null){
            return ResponseBean.error("回显失败");
        }else {
            return ResponseBean.success(Supplier);
        }
    }

    //编辑物资来源(更改保存)
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,@RequestBody SupplierVO SupplierVO){
        int i = productService.update(id,SupplierVO);
        if (i == 0){
            return ResponseBean.error("更新失败");
        }else {
            return ResponseBean.success();
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        int i = productService.delete(id);
        if (i == 0){
            return ResponseBean.error("删除失败");
        }else {
            return ResponseBean.success();
        }
    }


    @GetMapping("/findAll")
    public ResponseBean findAll(){
        List<Supplier> productList = productService.findAll();
        return ResponseBean.success(productList);
    }

    //business/product/findProductList?pageNum=1&pageSize=6&name=&categoryId=&supplier=&status=0
    //business/supplier/findSupplierList?pageNum=1&pageSize=10&name=
}
